package com.baturu.vin.gm.transformer;

import com.baturu.kit.kit.function.SafeFunction;
import com.baturu.vin.dto.EPCPartsImageDTO;
import com.baturu.vin.dto.superEPC.SEPCGroupDTO;
import com.baturu.vin.dto.superEPC.SEPCGroupTipDTO;
import com.baturu.vin.gm.dal.entity.GroupDescEntity;
import com.baturu.vin.gm.dal.entity.SubjectDescEntity;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: chenjiahao
 * @Time: 2019/11/5
 */
public class GMSEPCGroupTransformer {
    private static Pattern chRex = Pattern.compile(" [\\u4E00-\\u9FA5]");

    public static final SafeFunction<GroupDescEntity, SEPCGroupDTO> GROUP_TO_SEPC = d->
            SEPCGroupDTO.builder()
                    .mainGroup(d.getGroupCode()+":"+d.getDescriptionCn())
                    .mainGroupCode(d.getGroupCode())
                    .build();

    public static final SafeFunction<SubjectDescEntity, SEPCGroupDTO> ENTITY_TO_DTO = d->{
        EPCPartsImageDTO epcPartsImageDTO = new EPCPartsImageDTO();
        epcPartsImageDTO.setImageCode(d.getSubject());
        String descriptionCn = StringUtils.replace(d.getDescriptionCn(),"amp","");
        Matcher chMatcher = chRex.matcher(descriptionCn);
        String m = "";
        if (chMatcher.find()) {
            m = chMatcher.group();
        }
        String groupDes = m + StringUtils.substringAfter(descriptionCn, m);
        String remark = StringUtils.substringBefore(descriptionCn, m);
        return SEPCGroupDTO.builder()
                .epcPartsImageDTOList(Lists.newArrayList(epcPartsImageDTO))
                .imageUrl(d.getSubject())
                .subGroupCode(d.getSubject())
                .subGroup(d.getSubject()+":"+groupDes)
                .tips(SEPCGroupTipDTO.builder().subGroupDesc(remark).subGroupNo(d.getSubject()+":"+groupDes).build())
                .subGroupDesc(remark)
                .build();
    };

}
